package state

import (
	"fmt"
	"luu/binchunk"
)

type closure struct {
	proto    *binchunk.Prototype
	goFunc   GoFunction
	upvalues []*upvalue
}

type upvalue struct {
	val *luaValue
}

func NewLuaClosure(proto *binchunk.Prototype) *closure {
	c := &closure{proto: proto}
	if upVlen := len(proto.UpValues); upVlen > 0 {
		c.upvalues = make([]*upvalue, upVlen)
	}
	return c
}

func NewGoClosure(goFunc GoFunction, nUpvals int) *closure {
	c := &closure{goFunc: goFunc}
	if nUpvals > 0 {
		c.upvalues = make([]*upvalue, nUpvals)
	}
	return c
}

func (this *closure) ToStr() string {
	if this.proto != nil {
		return fmt.Sprintf("f_%d_%d", this.proto.LineDefined, this.proto.LastLineDefined)
	}
	if this.goFunc != nil {
		return "gf"
	}
	return ""
}
